package com.example.spring.controller;

import com.example.spring.entity.GS;
import com.example.spring.entity.Good;
import com.example.spring.entity.History;
import com.example.spring.mapper.GoodMapper;
import com.example.spring.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/history")
public class HistoryController {

    @Autowired
    GoodMapper goodMapper;
    @Autowired
    GoodService goodService;

    @PostMapping("/write")
    public void read(@RequestParam Integer whid,
                     @RequestParam Timestamp time,
                     @RequestParam Integer dataNo,
                     @RequestBody Map<String, List<GS>> data0,
                     HttpServletRequest request,
                     HttpServletResponse response)
    {
        List<GS> data = data0.get("data");
        Iterator<GS> itr = data.iterator();
        GS tmp;
        while(itr.hasNext()) {
            tmp = itr.next();
            Integer gid = tmp.getGid();
            Integer sid = tmp.getSid();
            goodService.readSlot1(gid, sid, whid, time);
        }

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 本机访问
        if ("localhost".equalsIgnoreCase(ip) || "127.0.0.1".equalsIgnoreCase(ip) || "0:0:0:0:0:0:0:1".equalsIgnoreCase(ip)){
            // 根据网卡取本机配置的IP
            InetAddress inet;
            try {
                inet = InetAddress.getLocalHost();
                ip = inet.getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (null != ip && ip.length() > 15) {
            if (ip.indexOf(",") > 15) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        History history = new History();
        history.setCreateTime(time);
        if (response.getStatus() == 200) history.setAcceptStatus("success");
        else history.setAcceptStatus("failed");
        history.setIp(ip);
        history.setWhid(whid);
        history.setWritenInStatus("success");
        history.setDataNo(dataNo);
        goodMapper.insert_history(history);
//        System.out.println(response.getStatus());
//        System.out.println(ip);
    }

    @PostMapping("/select")
    public Map<String, Object> selectHistory(@RequestParam Integer whid,
                                             @RequestParam Integer pageNum,
                                             @RequestParam Integer pageSize){
        pageNum = (pageNum - 1) * pageSize;
        List<History> data =  goodMapper.showHistoryPage(whid, pageNum, pageSize);
        Integer total = goodMapper.showHistoryTotal(whid);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
}
